define(function() {
	return function($compile, $scope) {
		require(
				[
						"PDUtilDir/util", "PDUtilDir/tool",
						"PDUtilDir/slidebar", "PDUtilDir/inputSelect",  "PDUtilDir/dialog",
				        "css!WebUploaderCss", 
				        "Cropper", 
				        "css!CropperCss",  "Date",
						"DateCN", "css!DateCss" ],
				function(Util, Tool,
						Slidebar, inputSelect,Dialog) {
					/* 查询 start */
					$scope.cache = {
						types : new Array(),
						teams : new Array(),
						typeMap: new Map(),
						certificates : new Array(),
						htProjects:new Array(),
						roles:new Array(),
						workTypes:new Array(),
						bdType:'',
					};
					$scope.entity = {
							projectCode : '',
							projectCodes:''
							
						};
			
					$scope.search = {
						zhName : "",
						enName : ""
					}
					$scope.searchSupport = {
							
						run : function() {
							$scope.grid.reload({
								queryParam : {
									zhName : $scope.search.zhName,
									enName : $scope.search.enName
								}
							})
						}
					};
					
					$scope.save = function(){
				
						var  idInfoObj = {
						     name :$("#name").val(),
							 folk:$("#folk").val(),
							 sex:$("#sex").val(),
							 birthDay:$("#birthDay").val(),
							 cardSn:$("#cardsn").val(),
							 address:$("#address").val(),
							 agency:$("#agency").val(),
							 validDuring:$("#valid").val(),
							 samid:$("#samid").val(),
							 idCardSn:$("#idCardSN").val(),
							 jpgPhotobuf:$("#JPGPhotobuf").attr('src'),
							 jpgHead:$("#capturePhoto").attr('src'),
//							 personNo:$scope.entity.personNo,
							 workerType:$scope.entity.workerType,
							 projectCode:$scope.entity.projectCodes,
							 team:$scope.entity.teamCode,
							 personRole:$scope.entity.roleCode
							 
							 
						}
					
						if(idInfoObj.cardSn==null||idInfoObj.cardSn==''){
							 Util.alert("请刷身份证，读取身份证信息");
							 return;
						}
						if(idInfoObj.personRole==null||idInfoObj.personRole==''){
							Util.alert("请选择角色");
							return;
						}
						if(idInfoObj.team==null||idInfoObj.team==''){
							Util.alert("请选择队伍");
							return;
						}
						if(($scope.cache.bdType!="BD000")&&(idInfoObj.workerType==null||idInfoObj.workerType=='')){
							Util.alert("请选择标段");
							return;
						}
						
						if(idInfoObj.projectCode==null||idInfoObj.projectCode==''){
							Util.alert("请选择项目");
							return;
						}
						
						$.ajax({
		                    url : getServer() + "/flea/hclv/person/saveOrUpdate.htmls",
		                    data : {
		                        'model' : JSON.stringify(idInfoObj)
		                    },
		                    success : function(data) {
		                        if (data.status != 200) {
		                            return;
		                        }
		                        Util.alert(data.message);
		                       
		                    }
						});
		
					
						
						
						
						
						
						
						
						
					}

					$scope.gridRender = function() {


				
						//加载项目
						inputSelect.dropCache();
						inputSelect({
							id : "projectName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							   multi : true,
							data : $scope.cache.htProjects,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : false,
							callback : function(id,data) {
								  var str = "";
							    $.each(data, function(i, n){
				                    str += n["dictCode"] + ",";
				                })
				                
								$scope.entity.projectCode = id[0];
							    $scope.entity.projectCodes = str;
								$scope.entity.projectName=id[1];
								
								
							}
						});
						//类型
						inputSelect.dropCache();
						inputSelect({
							id : "roleName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.roles,
							simpleData : true,
							// 启用文本搜索
							 initData:['D_HT_DITIE_ROLE_A'],
							searchAble : false,
							callback : function(id) {
								$scope.entity.roleCode = id[0];
								$scope.entity.roleName = id[1];
							}
						});
						//队伍
						inputSelect.dropCache();
						inputSelect({
							id : "teamName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.teams,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : true,
							callback : function(id) {
								$scope.entity.teamCode = id[0];
								$scope.entity.teamName = id[1];
							}
						});
						//类型
						inputSelect.dropCache();
						inputSelect({
							id : "workerTypeName",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.workTypes,
							simpleData : true,
							// 启用文本搜索
							 initData:['D_HT_DITIE_WORK_TYPE_NULL'],
							searchAble : false,
							callback : function(id) {
								$scope.entity.workerType = id[0];
								$scope.entity.workerTypeName = id[1];
							}
						});
						$("#birthDate").datetimepicker({
				            //设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
				            language : "cn",
				            //输出格式化
				            format : 'yyyy-mm-dd',
				            //直接选择‘今天’
				            todayBtn : true,
				            //设置最精确的时间选择视图
				            minView : 'month',
				            //高亮当天日期
				            todayHighlight : true,
				            //选择完毕后自动关闭
				            autoclose : true
				        });
			
					/*	_this.$scope.$watch("addSupport.entity.birthDate", function(value){
							_this.entity.age = Common.getAgeByBirthday(value);
						});*/
					
					};
					
					var init = function() {
						$.ajax({
							url : getServer() + "/flea/hclv/getFaceRoleType.htmls",
							success : function(data) {
								$scope.cache.roles = data.data;
								// 证件类型
//								inputSelect.dropCache();
//								inputSelect({
//									id : "queryType",
//									key : {
//										id : "dictCode",
//										name : "dictName",
//										data : "data",
//									},
//									data : $scope.cache.certificates,
//									simpleData : true,
//									// 启用文本搜索
//									// initData:[initProvice],
//									searchAble : false,
//									callback : function(id) {
//										$scope.search.type = id[0];
//									}
//								});
							}
						});
						//游客类型
			
						$.ajax({
							url : getServer() + "/flea/hclv/getDictHTProject.htmls",
							success : function(data) {
								$scope.cache.htProjects = data.data;
								$scope.gridRender();
							}
						});
						$.ajax({
							url : getServer() + "/flea/hclv/getTeams.htmls",
							success : function(data) {
								$scope.cache.teams = data.data;
								$scope.gridRender();
							}
						});
						$.ajax({
							url : getServer() + "/flea/hclv/getDictWorkType.htmls",
							success : function(data) {
								$scope.cache.workTypes = data.data;
								$scope.gridRender();
							}
						});
						$.ajax({
							url : getServer() + "/flea/hclv/getUserBD.htmls",
							success : function(data) {
								$scope.cache.bdType = data.data;
								if(data.data=="BD000"){
									$("#bd").show();
								}
								
								
								$scope.gridRender();
							}
						});
						$scope.gridRender();
					}
					
					init();
				})
	}
});